<?php
/**
* 权限
* @author zlj <389843954@qq.com>
*/
namespace app\common\model;
use think\Model;
class Permission extends Model
{
	
	/**
	 * 获取列表
	 * @param  [array] $where    [条件]
	 * @return [type]            [description]
	 */
	public function perlist($where=[]){
      	
        if ($where){
            $result = self::where($where);
        }else{
        	$result = self::select();
        }

        return $result;
	}

	/**
	 * 获取组权限
	 * @param  [string] $gid    [用户组id]
	 * @return [type]            [description]
	 */
	public function gropuPre($gid){
      	
      	$field = 'p.id as pid,m.*';
      	$result = self::alias('p')->join('menu m','m.id=p.mid','left')->field($field)->where('group_id',$gid)->select();

        return $result;
	}



	/**
	 * 添加权限
	 * @param [string] $gid [用户组id]
	 * @param [array] $mid [菜单id]
	 */
	public function addPer($gid,$mid){
		if (is_array($mid) && !empty($mid) && $gid>0){
            //先删除
            self::where(array('group_id'=>$gid))->delete();

            $data['group_id'] = $gid;
            foreach ($mid as $key => $val){
                $data['mid'] = $val;

                $res = self::insert($data);
                if(!$res){
                    return false;
                }
            }
            return true;
        }
	}
}
